package com.fr.base.core.util;

import com.fr.base.BaseUtils;
import com.fr.base.FRContext;
import com.fr.report.script.Calculator;
import com.fr.report.script.ParameterMapNameSpace;
import com.fr.util.Utils;
import com.fr.web.core.WebUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletResponse;

public class TemplateUtils
{
  public static Pattern ParameterPattern = Pattern.compile("\\$\\{((\\\"[^(\\$\\{)]*\\}[^(\\$\\{)]*\\\")|(\\\"[^(\\$\\{)]*[^\\$]?\\{[^(\\$\\{)]*\\\")|[^\\{\\}])*+\\}");

  public static String readTemplate2String(String paramString1, String paramString2)
  {
    StringWriter localStringWriter = new StringWriter();
    InputStream localInputStream = BaseUtils.readResource(paramString1);
    try
    {
      InputStreamReader localInputStreamReader = new InputStreamReader(localInputStream, paramString2);
      Utils.copyCharTo(localInputStreamReader, localStringWriter);
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      FRContext.getLogger().log(Level.WARNING, localUnsupportedEncodingException.getMessage(), localUnsupportedEncodingException);
    }
    catch (IOException localIOException)
    {
      FRContext.getLogger().log(Level.WARNING, localIOException.getMessage(), localIOException);
    }
    return localStringWriter.getBuffer().toString();
  }

  public static String render(String paramString1, String paramString2, Object paramObject)
  {
    return render(paramString1, new String[] { paramString2 }, new Object[] { paramObject });
  }

  public static String render(String paramString, String[] paramArrayOfString, Object[] paramArrayOfObject)
  {
    HashMap localHashMap = new HashMap();
    for (int i = 0; i < paramArrayOfString.length; ++i)
      localHashMap.put(paramArrayOfString[i], paramArrayOfObject[i]);
    try
    {
      return render(paramString, localHashMap);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return paramString;
  }

  public static String render(String paramString, Map paramMap)
    throws IOException, Exception
  {
    Calculator localCalculator = Calculator.createCalculator();
    localCalculator.pushNameSpace(ParameterMapNameSpace.create(paramMap));
    return localCalculator.renderTpl(paramString);
  }

  public static String render(StringReader paramStringReader, Map paramMap)
    throws IOException
  {
    BufferedReader localBufferedReader = new BufferedReader(paramStringReader);
    StringBuffer localStringBuffer = new StringBuffer();
    while (localBufferedReader.readLine() != null)
      localStringBuffer.append(localBufferedReader.readLine());
    String str = "";
    try
    {
      str = render(localStringBuffer.toString(), paramMap);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return str;
  }

  public static String renderTemplate(String paramString, Map paramMap)
    throws IOException
  {
    StringWriter localStringWriter = new StringWriter();
    PrintWriter localPrintWriter = new PrintWriter(localStringWriter);
    dealWithTemplate(paramString, localPrintWriter, paramMap);
    return localStringWriter.toString();
  }

  public static void dealWithTemplate(String paramString, HttpServletResponse paramHttpServletResponse, Map paramMap)
    throws IOException
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    dealWithTemplate(paramString, localPrintWriter, paramMap);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  public static void dealWithTemplate(String paramString, PrintWriter paramPrintWriter, Map paramMap)
    throws IOException
  {
    InputStream localInputStream = BaseUtils.readResource(paramString);
    if (localInputStream == null)
      throw new IOException("Not found template: " + paramString);
    dealWithTemplate(localInputStream, "GBK", paramPrintWriter, paramMap);
    localInputStream.close();
  }

  public static void dealWithTemplate(InputStream paramInputStream, String paramString, PrintWriter paramPrintWriter, Map paramMap)
    throws IOException
  {
    BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(paramInputStream, paramString));
    dealWithTemplate(localBufferedReader, paramPrintWriter, paramMap);
    localBufferedReader.close();
  }

  public static void dealWithTemplate(Reader paramReader, PrintWriter paramPrintWriter, Map paramMap)
    throws IOException
  {
    BufferedReader localBufferedReader = new BufferedReader(paramReader);
    StringBuffer localStringBuffer = new StringBuffer();
    String str1 = "";
    while ((str1 = localBufferedReader.readLine()) != null)
    {
      if (localStringBuffer.length() > 0)
        localStringBuffer.append('\n');
      localStringBuffer.append(str1);
    }
    String str2 = "";
    try
    {
      str2 = render(localStringBuffer.toString(), paramMap);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    paramPrintWriter.write(str2);
  }
}